package cn.tianyigps.tianyigpsapiv2.controller;

import cn.tianyigps.tianyigpsapiv2.param.enums.ResponseEnum;
import cn.tianyigps.tianyigpsapiv2.param.vo.ResponseVo;
import cn.tianyigps.tianyigpsapiv2.service.EquipmentSubscriberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: LiuHao
 * @Descirption: 设备订阅
 * @Date: 2019/4/26_17:11
 */
@Slf4j
@RestController
@RequestMapping("/equipment/subscriber")
@CrossOrigin
@Api(value = "EquipmentSubscriberController", description = "设备订阅接口")
public class EquipmentSubscriberController {

    @Autowired
    private EquipmentSubscriberService equipmentSubscriberService;

    /**
     * 设备订阅设置
     * @param equipmentId
     * @param name
     * @param map ｛"name":"","keys":["key1","key2"],"task":Task对象｝
     * @return
     */
    @PostMapping("/{equipmentId}/add/{name}")
    @ApiOperation(value = "设备订阅设置", notes = "测试接口")
    public ResponseVo add(@PathVariable String equipmentId,@PathVariable String name, @RequestBody Map map){
        log.info("设备订阅新增，equipmentId==>{}，name==>{},map==>{}",equipmentId,name,map);
        equipmentSubscriberService.add(equipmentId,name,map);
        return ResponseVo.success();
    }


    @DeleteMapping("/{equipmentId}/del/{name}")
    @ApiOperation(value = "设备订阅删除", notes = "测试接口")
    public ResponseVo del(@PathVariable String equipmentId, @PathVariable String name){
        log.info("设备订阅删除，equipmentId==>{}，name==>{}",equipmentId,name);
        boolean success = equipmentSubscriberService.del(equipmentId,name);
        if(!success){
            return ResponseVo.response(ResponseEnum.DELETE_FAILURE);
        }
        return ResponseVo.success();
    }

    @GetMapping("/{equipmentId}/list")
    @ApiOperation(value = "设备订阅列表", notes = "测试接口")
    public ResponseVo list(@PathVariable String equipmentId){
        log.info("设备订阅删除，equipmentId==>{}",equipmentId);
        Map subscriber = equipmentSubscriberService.list(equipmentId);
        return ResponseVo.response(ResponseEnum.QUERY_SUCCESS,subscriber);
    }
}
